package com.lemon.exam.common.config;

import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Elasticsearch 配置类
 *
 * @author Lemon
 * @since 2025/4/25 18:17
 */
@Configuration
public class ElasticsearchConfiguration {
    @Bean
    public ElasticsearchTransport elasticsearchTransport(ElasticsearchProperties elasticsearchProperties, ObjectMapper objectMapper) {
        HttpHost[] hostArray = elasticsearchProperties.getUris().parallelStream().map(HttpHost::create).toArray(HttpHost[]::new);
        RestClient restClient = RestClient.builder(hostArray).build();
        JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper(objectMapper);
        return new RestClientTransport(restClient, jsonpMapper);
    }
}
